package com.jerry.huihome.module.find.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseApplication;
import com.jerry.huihome.base.BaseViewHolder;
import com.jerry.huihome.base.MyBaseAdapter;
import com.jerry.huihome.module.find.activity.ForemanDetailActivity;
import com.jerry.huihome.module.find.entity.Foreman;
import com.jerry.huihome.views.RoundImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/10.
 */
public class PermanentAdapter extends MyBaseAdapter<Foreman.BodyBean.PmsBean> {
    private Context context;
    private int screenWidth;
    private LinearLayout.LayoutParams layoutParams;
    private final DisplayImageOptions options;

    public PermanentAdapter(Context context, List<Foreman.BodyBean.PmsBean> list) {
        super(context, list);
        this.context = context;
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager windowManager =
                (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(metrics);
        // 屏幕宽度
        screenWidth = metrics.widthPixels;
        layoutParams = new LinearLayout.LayoutParams((int) (screenWidth /4.4),260);
        options = new DisplayImageOptions.Builder().cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .build();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_construction;
    }

    @Override
    public BaseViewHolder getViewHolder() {
        return new ViewHolder() ;
    }

    @Override
    public void bindViews(View view, BaseViewHolder viewHolder) {
        final Foreman.BodyBean.PmsBean item = getItem(viewHolder.position);
        ViewHolder mViewHolder = (ViewHolder) viewHolder;
        mViewHolder.ratingBar.setRating(item.getTotalScore());
        mViewHolder.tvSuccessNum.setText(item.getCaseCount()+"");
        mViewHolder.tvPermanentName.setText(item.getRealname());
        if (item.getAuthFlag()==0){
            mViewHolder.tvPermanentAttest.setText("已认证");
        }else {
            mViewHolder.tvPermanentAttest.setText("未认证");
        }
        if (item.getWarrantFlag() == -1){
            mViewHolder.tvPermanentVouch.setText("未担保");
        }else {
            mViewHolder.tvPermanentVouch.setText("已担保");
        }
        mViewHolder.tvWorkYears.setText(item.getWorkYear()+"");
        BaseApplication.getApplication().getImageLoader().displayImage(item.getSAvatar(),mViewHolder.ivDesignersHeadImg,options);
       /* mViewHolder.ivHeadBackground.setVisibility(View.VISIBLE);
        mViewHolder.ivHeadBackground.setImageResource(R.drawable.factory_icon);*/
        List<Foreman.BodyBean.PmsBean.AbbrWorksBean> abbrWorksBeen = item.getAbbrWorks();
        ImageView imageView = null;
        if (abbrWorksBeen.size() == 0){
            mViewHolder.llPic.setVisibility(View.GONE);
        }
        for (int i = 0; i <abbrWorksBeen.size() ; i++) {
            layoutParams.leftMargin = 20;
            layoutParams.topMargin = 20;
            layoutParams.bottomMargin = 20;
            if (i == abbrWorksBeen.size() - 1)
            {
                layoutParams.rightMargin = 10;
            }
            imageView = new ImageView(context);
            imageView.setLayoutParams(layoutParams);
            Picasso.with(context)
                    .load(abbrWorksBeen.get(i).getImgUrl()).fit()
                    .into(imageView);

            mViewHolder.llPic.addView(imageView);
            mViewHolder.llClick_foreman.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context,ForemanDetailActivity.class);
                    int pmid = item.getUid();
                    intent.putExtra("pmid",pmid);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            });
        }
    }

    class ViewHolder extends BaseViewHolder{
        @BindView(R.id.iv_designers_head_img)
        RoundImageView ivDesignersHeadImg;
      /*  @BindView(R.id.iv_head_background)
        ImageView ivHeadBackground;*/
        @BindView(R.id.tv_permanent_name)
        TextView tvPermanentName;
        @BindView(R.id.ratingBar)
        RatingBar ratingBar;
        @BindView(R.id.tv_work_years)
        TextView tvWorkYears;
        @BindView(R.id.tv_permanent_attest)
        TextView tvPermanentAttest;
        @BindView(R.id.tv_permanent_vouch)
        TextView tvPermanentVouch;
        @BindView(R.id.tv_success_num)
        TextView tvSuccessNum;
        @BindView(R.id.ll_pic)
        LinearLayout llPic;
        @BindView(R.id.ll_click_foreman)
        LinearLayout llClick_foreman;

    }
}
